#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>

#define SPACE 's'
#define RED 'r'
#define BLUE 'b'
#define GREEN 'g'

#define DEBUG 0

// Flag vector to signal the end of the wins in the wins array
#define END_OF_WINS 0	

int columns;
int rows;
// NOTE: last_move starts at 1, not 0. This happens to be beneficial because, with padding, the first column is board[1]
int last_move;
int total_time;
int player_1_time;
int last_move_time;

// The Wins Lookup Table
char WINS_LOOKUP[12][4] = {"ggrr","grgr","rgrg","rrgg","grrg","rggr","ggbb","gbgb","bgbg","bbgg","gbbg","bggb"};

FILE *f;



/*takes wins as a n by 4 2D array and looks at the different wins and evaluates which player won
and their point values

This function has to do 3 things:
1) figure out which player won - return positive or negative values
2) Figure out what point value they won with - arrangment of the peices
3) If two players have the same winning value - return 1 point
*/
int evalWin(char wins[150][4])
{
	
	int i = 0;
	int points = 0;
	int bestwin = 0;
	int worstwin = 0;
	int redpoints = 0;
	int bluepoints = 0;
	do{
		////fprintf(f, f, "checking case %s\n", wins[i]);
		//check for tie - if there is nothing in the wins array
		if(wins[0][0] == '\0' || wins[0][0] == 0)
		{
			////fprintf(f, "0");//no wins in array - no nothing in array
			points = 0;
			return 0;
		}
		else//we have at least one win - check it out
		{
			//first - check for the good wins - where the first two char's will match
			if(wins[i][0] == wins[i][1])//same colour first two pieces
			{
				
				if(wins[i][0] == 'r')
				{
					redpoints += 5;
					points = -5;
				}//we won
				else if(wins[i][0] == 'b')
				{
					bluepoints += 5;
					points = 5;
				}
				
				else if(wins[i][0] == 'g')//don't know who won yet
				{
					if(wins[i][2] == 'r')
					{
						redpoints += 5;
						points = -5;
					}//we won
					else
					{
						bluepoints += 5;
						points = 5;
					}
				}
			}
			
			else if(wins[i][1] == wins[i][2])//middle two are same colour - 4 points
			{
				
				if(wins[i][0] == 'r')
				{
					redpoints += 4;
					points = -4;
				}//we won
				else if(wins[i][0] == 'b')
				{
					bluepoints += 4;
					points = 4;
				}
				else//first piece is green
				{//check the second piece
					if(wins[i][1] == 'r')
					{
						redpoints += 4;
						points = -4;
					}//we won  
					else if(wins[i][1] == 'b')
					{
						bluepoints += 4;
						points = 4;
					}
					
				}
				
			}
			else //3 points will be awarded
			{
				if(wins[i][0] == 'r')
				{
					redpoints += 3;
					points = -3;
				}//we won
				else if(wins[i][0] == 'b')
				{
					bluepoints += 3;
					points = 3;
				}
				else//first piece is green
				{//check the second piece
					if(wins[i][1] == 'r')
					{
						redpoints += 3;
						points = -3;
					}//we won
					else if(wins[i][1] == 'b')
					{
						bluepoints += 3;
						points = 3;
					}
				}
			}
			
			
			
		}
		if(points > 0)
		{
			if(bestwin < points)
				bestwin = points;
		}
		else
			if(worstwin > points)
			worstwin = points;
		
		i++;
	}while(wins[i][0] != 0);
	
	
	if( redpoints - bluepoints > 0)
		points = worstwin;
	else if(redpoints - bluepoints == 0)
		points = 1;
	else
		points = bestwin;
	
	////fprintf(f,  "%d\n", points);
	return points;
}

// (7,6,3,600,290,20,r,b,s,s,s,s,b,b,r,g,s,s,b,r,b,b,g,s,r,g,r,b,s,s,r,r,b,r,r,s,s,s,s,s,s,s,s,s,s,s,s,s)
int readBoard(char board[columns+2][rows+2])
{
	int i, j, p = 0;
	
	// Populate board array
	for (i = 0; i < columns+2; i++)
	{
		// Padding columns on left and right sides of board
		if (i == 0 || i == columns+1)
		{
			for(j = 0; j < rows+2; j++)
				board[i][j] = SPACE;
			continue;
		}
		// Padding Row over bottom of board
		board[i][0] = SPACE;
		for (j = 1; j < rows+1; j++)
		{
			scanf(",%c", &board[i][j]);
			if(board[i][j] < 's')
				p++;
		}
		// Padding row over top of board
		board[i][j] = SPACE;
	}
	return p;
}

void findWins(char board[columns+2][rows+2], char wins[150][4], int last_played)
{
	// last_piece_x and last_piece_y hold co-ordinates of the last played piece, that is the piece that must be checked for wins
	int last_piece_x = last_played, last_piece_y, winIndex = 0;
	int i;
	// get the last piece's Y co-ordinate
	for (i=1;i<rows+1;i++)
	{
		if(board[last_piece_x][i] == SPACE)
			break;
		last_piece_y = i;
	}
	
	//fprintf(f, "Last Moved Piece's co-ordinates:%i, %i, %c\n", last_piece_x, last_piece_y, board[last_piece_x][last_piece_y]);  
	
	/*
	* How this works is: For each directional axis (X, Y, Diagonal Down Left-to-Right, and Diagonal Up Left-to-Right), the last dropped
	* piece can be in 4 vectors (it can be in positions 0-3). For each of these possible positions, the algorithm grabs the 4 pieces
	* and then checks that vector to see if it is a win. If so, it adds it to the wins array.
	*/
	
	// Diagonal Top Left<->Bottom Right
	//fprintf(f, "Beginning Diagonal Loop\n");
	for(i=0;i<4;i++) // Check each of the 4 possible positions for the last dropped piece
	{
		short int offset;
		short int size = 1;
		
		//fprintf(f, "for position:%i\n", i);
		
		// The co-ordinates used to build the vector to check on. Note these move diagonally to the right,down.
		short int start_piece_x = last_piece_x + i;
		short int start_piece_y = last_piece_y - i;
		//fprintf(f, "start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
		// If the starting piece is a space, stop moving in that direction
		if (board[start_piece_x][start_piece_y] == SPACE)
		{
			//fprintf(f, "BREAK1: Found Space, finished looking at diagonal!\n", i);
			break;
		}
		char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
		// Build a vector going diagonally to the up, left
		for (offset = 1;offset < 4;offset++)
		{
			if (board[start_piece_x-offset][start_piece_y+offset] == SPACE)
			{
				////fprintf(f, "BREAK2: Found Space While building a vector\n");
				break;
			}  
			win[size++] = board[start_piece_x-offset][start_piece_y+offset];
		}
		if (size == 4) // Found a four piece vector (No Spaces)
		{
			////fprintf(f, "Successfully Found a Four-Piece Vector: %s\n", win);
			int k;
			for (k=0;k<12;k++)
			{
				if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
				{
					////fprintf(f, "Found a Win! Vector is %s\n", win);
					strcpy(wins[winIndex++], win);
				}
			}
			
		}
	}
	// Top<->Bottom
	////fprintf(f, "Beginning Vertical Loop\n");
	for(i=0;i<4;i++)
	{
		short int offset, size = 1;
		////fprintf(f, "for position:%i\n", i);
		short int start_piece_x = last_piece_x, start_piece_y = last_piece_y - i;
		////fprintf(f, "start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
		if (board[start_piece_x][start_piece_y] == SPACE)
		{
			////fprintf(f, "BREAK1: Found Space, finished looking at Vertical!\n", i);
			break;
		}
		
		char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
		// Build a vector going diagonally to the up, left
		for (offset = 1;offset < 4;offset++)
		{
			if (board[start_piece_x][start_piece_y+offset] == SPACE)
			{
				////fprintf(f, "BREAK2: Found Space While building a vector\n");
				break;
			}
			win[size++] = board[start_piece_x][start_piece_y+offset];
		}
		if (size == 4) // Found a four piece vector (No Spaces)
		{
			////fprintf(f, "Successfully Found a Four-Piece Vector: %s\n", win);
			int k;
			for (k=0;k<12;k++)
			{
				if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
				{
					////fprintf(f, "Found a Win! Vector is %s\n", win);
					strcpy(wins[winIndex++], win);
				}
			}
			
		}
	}
	// Diagonal Bottom Left<->Top Right
	////fprintf(f, "Beginning 2nd Diagonal Loop\n");
	for(i=0;i<4;i++)
	{
		short int offset, size = 1;
		////fprintf(f, "for position:%i\n", i);
		
		short int start_piece_x = last_piece_x - i, start_piece_y = last_piece_y - i;
		////fprintf(f, "start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
		if (board[start_piece_x][start_piece_y] == SPACE)
		{
			////fprintf(f, "BREAK1: Found Space, finished looking at 2nd diagonal!\n", i);
			break;
		}
		
		char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
		// Build a vector going diagonally to the up, left
		for (offset = 1;offset < 4;offset++)
		{
			if (board[start_piece_x+offset][start_piece_y+offset] == SPACE)
			{
				////fprintf(f, "BREAK2: Found Space While building a vector\n");
				break;
			}
			
			win[size++] = board[start_piece_x+offset][start_piece_y+offset];
		}
		if (size == 4) // Found a four piece vector (No Spaces)
		{
			////fprintf(f, "Successfully Found a Four-Piece Vector: %s\n", win);
			int k;
			for (k=0;k<12;k++)
			{
				if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
				{
					////fprintf(f, "Found a Win! Vector is %s\n", win);
					strcpy(wins[winIndex++], win);
				}
			}
			
		}
	}
	//  Left<->Right
	////fprintf(f, "Beginning Horizontal Loop\n");
	for(i=0;i<4;i++)
	{
		short int offset, size = 1;
		////fprintf(f, "for position:%i\n", i);
		
		short int start_piece_x = last_piece_x - i, start_piece_y = last_piece_y;
		////fprintf(f, "start_piece co-ordinates are: %i, %i\n", start_piece_x, start_piece_y);
		if (board[start_piece_x][start_piece_y] == SPACE)
		{
			////fprintf(f, "BREAK1: Found Space, finished looking at Horizontal!\n", i);
			break;
		}
		
		char win[4] = {board[start_piece_x][start_piece_y],'s','s','s'};
		// Build a vector going diagonally to the up, left
		for (offset = 1;offset < 4;offset++)
		{
			if (board[start_piece_x+offset][start_piece_y] == SPACE)
			{
				////fprintf(f, "BREAK2: Found Space While building a vector\n");
				break;
			}
			
			win[size++] = board[start_piece_x+offset][start_piece_y];
		}
		if (size == 4) // Found a four piece vector (No Spaces)
		{
			////fprintf(f, "Successfully Found a Four-Piece Vector: %s\n", win);
			int k;
			for (k=0;k<12;k++)
			{
				if (strncmp(win,WINS_LOOKUP[k], 4) == 0)
				{
					////fprintf(f, "Found a Win! Vector is %s\n", win);
					strcpy(wins[winIndex++], win);
				}
			}
			
		}
	}
	// End with "end of wins" flag vector, to signal the end of wins
	wins[winIndex][0] = END_OF_WINS;
}

// Print Board function for debugging
void printBoard(char board[columns+2][rows+2])
{
	#if DEBUG
	int i, j;
	// TEST: Print out board
	for (i = 0; i < rows+2; i++)
	{
		fprintf(f, "\t");
		for (j = 0; j < columns+2; j++)
		{
			fprintf(f, "%c ", board[j][i]);
		}
		fprintf(f, "\n");
	}
	fprintf(f, "\n");
	#endif
}

int isWin(char board[columns+2][rows+2], int last_played)
{
	char wins[150][4];
	findWins(board, wins, last_played);
	return evalWin(wins);
	
}

// guess() is the controller for the heuristics. guess() builds all possible vectors for the board and then calls the heuristic(s) for each and totals the score, which is returned.
//(10,7,7,2156,0,443,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,r,s,s,s,s,s,s,b,g,r,s,s,s,s,b,b,r,s,s,s,s,b,r,g,r,s,s,s,r,r,b,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s)
int guess(char board[columns+2][rows+2])
{
	/*
	REMEMBER: 	Any changes made to how a vector is processed must be applied to all four directions.
	*/
	// Variables
	int x, y; // X and Y Co-Ordinates of each piece
	int xi, yi; // X and Y Offset Co-Ordinates for building the vector on piece X,Y
	int i; // 4 Piece Loop Counter
	
	int count = 0; // For debugging, a count of how many vectors are grabbed and checked
	
	/* Score Variables ***************************************************/
	int score; 	// The total score for the board
	int score_heuristic_1 = 0; // The Heuristic score for this board configuration
	
	// TODO: Add score variables for other heuristics here
	
	/*********************************************************************/
	
	// Go Through Every Piece (or Space) and check each of the four possible vectors built on that piece
	for (x = 1;x < columns + 1;x++)
	{
		for (y = 0; y < rows + 1;y++)
		{
			//int vectorIndex = 0;
			char vector[4] = {0, 0, 0, 0};			
			
			// 1) Straight Up (Y+1)
			if (!(y+3 > rows))
			{
				vector[0] = board[x][y];
				vector[1] = board[x][y+1];
				vector[2] = board[x][y+2];
				vector[3] = board[x][y+3];
				
				//vectorIndex = (vector[0] << 3) + (vector[1] << 2) + (vector[2] << 1) + vector[3];
				score_heuristic_1 += heuristic_1(vector);
			}
			// 2) Diagonal Up, Left to Right (X+1,Y+1)
			if (!((y+3 > rows) || (x+3 > columns)))
			{
				vector[0] = board[x][y];
				vector[1] = board[x+1][y+1];
				vector[2] = board[x+2][y+2];
				vector[3] = board[x+3][y+3];
				
				//vectorIndex = (vector[0] << 3) + (vector[1] << 2) + (vector[2] << 1) + vector[3];
				score_heuristic_1 += heuristic_1(vector);
			}
			// 3) Straight Right (X+1)
			if (!(x+3 > columns))
			{
				vector[0] = board[x][y];
				vector[1] = board[x+1][y];
				vector[2] = board[x+2][y];
				vector[3] = board[x+3][y];
				
				//vectorIndex = (vector[0] << 3) + (vector[1] << 2) + (vector[2] << 1) + vector[3];
				score_heuristic_1 += heuristic_1(vector);
			}
			// 4) Diagonal Down, Left to Right (X+1, Y-1)
			if (!((y-3 < 1) || (x+3 > columns)))
			{
				vector[0] = board[x][y];
				vector[1] = board[x+1][y-1];
				vector[2] = board[x+2][y-2];
				vector[3] = board[x+3][y-3];
				

				//vectorIndex = (vector[0] << 3) + (vector[1] << 2) + (vector[2] << 1) + vector[3];
				score_heuristic_1 += heuristic_1(vector);
			}
		}
	}
	//printf("\n\nTotal Vector count for %i X %i board: %i\n", columns, rows, count);
	
	/* TODO: Combine all heuristic scores together here. *****************/
	// Could do a weighted average, if there is a heuristic we trust more than others.
	score = score_heuristic_1;
	/*********************************************************************/
	#if DEBUG
		//fprintf(f, "\tTrying Heuristic. Score returned is: %i\n", score);
	#endif
	return score;
}

// First Heuristic. Uses Lookup table to get the value of that vector, then weights the result and returns a score
int heuristic_1(char vector[4])
{
	/* Heuristic Weights **************************/
	const int ONE_AWAY_MULTIPLIER = 10000;
	const int TWO_AWAY_MULTIPLIER = 1000;
	/**********************************************/
	
	int vectorScore = 0;

	int k, greenCount = 0, blueCount = 0, redCount = 0, spaceCount = 0;
	for (k=0;k<4;k++)
	{
		if (vector[k] == 'g')
			greenCount++;
		else if (vector[k] == 'r')
			redCount++;
		else if (vector[k] == 'b')
			blueCount++;
		else if (vector[k] == 's')
			spaceCount++;
	}

	/* 1-Aways ***********************************/
	// Blue
	if (spaceCount == 1 && ((greenCount == 2 && blueCount == 1) || (greenCount == 1 && blueCount == 2)))
		vectorScore = 2*ONE_AWAY_MULTIPLIER;
	// Red
	else if (spaceCount == 1 && ((greenCount == 2 && redCount == 1) || (greenCount == 1 && redCount == 2)))
		vectorScore = -2*ONE_AWAY_MULTIPLIER;

	/* 2-Aways ***********************************/
	// Blue	
	else if (spaceCount == 2 && ((greenCount == 1 && blueCount == 1) || blueCount == 2))
		vectorScore = 1*TWO_AWAY_MULTIPLIER;
	// Red						   
	else if (spaceCount == 2 && ((greenCount == 1 && redCount == 1) || redCount == 2))
		vectorScore = -1*TWO_AWAY_MULTIPLIER;

	// TODO: What should a 2-Green 2-Space vector be scored? It can be beneficial for both players
	// For now, I'll leave it at 0 -Nick
	//else if (spaceCount == 2 && greenCount == 2)
		//points[(board[i] << 3) + (board[i+1] << 2) + (board[i+2] << 1) + board[i+3]] = 0;
	
	return vectorScore;	
}

/*
*
*	Tested and works correctly
*
*/

int checkCol(char board[columns+2][rows+2], int col)
{  
	int i = 0;
	
	if(board[col][rows] != SPACE)
		return 0;//if the top of the col isn't a space - it is full
	else
		for(i = rows-1; i > 0; i--)
		{
			if(board[col][i] == SPACE)
				continue;
			else
				return i+1;
		}
		return i+1;
}
// (8,7,5,0,0,0,
//s,s,s,s,s,s,s,
//s,s,s,s,s,s,s,
//s,s,s,s,s,s,s,
//s,s,s,s,s,s,s,
//g,s,s,s,s,s,s,
//g,b,s,s,s,s,s,
//s,s,s,s,s,s,s,
//b,g,s,s,s,s,s)

//Implements Minimax search - lowest level returns the min, then the upper levels take the
//max value of the returned mins
int minMax( char board[columns+2][rows+2], int depth)
{
	int max_depth = 5;
	
	max(board, 5);  
}

int max(char board[columns+2][rows+2], int depth, int col){
	
	int best = -5, bestcol = 0;
	int val = 0;
	int i = 0;
	int row = 0;
	
	if(depth <= 0)
		//return guess(board);
		return 0;
	
	for(i = col-3; i < col+3; i++)
	{
		if(i <= 0)
			i = 1;
		else if(i > columns)
			break;
		if((row = checkCol(board, i)) == 0)
			continue;//col is full
		board[i][row] = BLUE;
		//printBoard(board);
		val = isWin(board, i);
		if(val == 5)
		{
			board[i][row] = SPACE;
			return (5 << depth);
		}
		else
			val = (val<<depth) + min(board, depth-1, i);
		if(val > best)
		{
			best = val;
			#if DEBUG
			//fprintf(f, "*****************************************updateing best to: %d", best);
			#endif
			bestcol = i;
		}
		
		board[i][row] = GREEN;
		//printBoard(board);
		#if DEBUG
		//fprintf(f, "depth = %d, col = %d, isWin = %d\n", depth, i, isWin(board, i));
		#endif
		val = isWin(board, i);
		//if a peice leads to a loss - stop recursing. Also if it leads to a best-case win stop.
		if(val < -2 || val  == 5 )
		{
			board[i][row] = SPACE;
			return (val<<depth);
		}
		else
			val = (val<<depth) + min(board, depth-1, i);
		if(val > best)
		{
			best = val;
			#if DEBUG
//			fprintf(f, "*****************************************updateing best to: %d\n", best);
			#endif
			bestcol = i;
		}
		board[i][row] = SPACE;
	}

	fprintf(f, "max returning %d in col %d\n", best, bestcol);

	return best;
	
	
}

int min(char board[columns+2][rows+2], int depth, int col)
{
	int best = 5, bestcol = 0;
	int val = 0;
	int i = 0;
	int row = 0;
	#if DEBUG	
//	fprintf(f, "Min with depth %d starts here.\n", depth);
	#endif
	if(depth <= 0)
		return 0;// guess(board);
	
	for(i = col-3; i < col+3; i++)
	{
		if(i <= 0)
			i = 1;
		else if(i > columns)
			break;
		
		if((row = checkCol(board, i)) == 0)
			continue;//col is full
		#if DEBUG
		//fprintf(f, "placeing a RED piece in column %d\n", i);
		#endif
		board[i][row] = RED;
		//printBoard(board);
		val = isWin(board, i);
		if(val == -5)
		{
			board[i][row] = SPACE;
			#if DEBUG
			//fprintf(f, "min returning -5 in col %d, with RED\n", i);
			#endif
			return -5 << depth ;
		}
		else
			val = (val<<depth) + max(board, depth-1, i);
		if(val < best)
		{
			#if DEBUG
			//fprintf(f, "Found a loss of:%d  in col %d with RED after that in depth: %d\n", val, i, depth);
			#endif
			best = val;
			bestcol = i;
		}
		
		board[i][row] = GREEN;
		#if DEBUG
		//fprintf(f, "placeing a GREEN piece in column %d\n", i);
		#endif
		//		printBoard(board);
		val = isWin(board, i);
		if(val == -5)
		{
			board[i][row] = SPACE;
			#if DEBUG
			//fprintf(f, "min returning -5 in col %d, with GREEN\n", i);
			#endif
			return (-5<<depth);//if opponent gets they best win, stop
		}
		else if(val > 2)//assume opponent won't play a green to help us...
		{
			board[i][row] = SPACE;
			continue;
		}
		else
			val = (val<<depth) + max(board, depth-1, i);
		if(val < best)
		{
			#if DEBUG
			//fprintf(f, "Found a loss of:%d  either in col %d with RED after that in depth: %d\n", val, i, depth);
			#endif
			best = val;
			bestcol = i;
		}
		board[i][row] = SPACE;
		
	}

	fprintf(f, "min returning %d in col %d\n", best, bestcol);

	return best;
}

int tryMove(char board[columns+2][rows+2], int col, char piece)
{

	int i = 0, row = -1, depth = 7, guess = 0, val = 0;
	// First, see if column has room for another piece
	//printBoard(board);

	
	if((row = checkCol(board, col)) == 0)
	{
		#if DEBUG
		fprintf(f, "\tColumn %i is Full.\n", col);
		#endif
		return -99999;	
	}
	else
	{
		board[col][row] = piece;
		/*	if(col < columns-3)
		{
		if(score[(board[col][row] << 3) + (board[col+1][row] << 2) + (board[col+2][row] << 1) + board[col+3][row]] < 0)
		return -pow(10, depth);
		}
		*/ 
		//		printBoard(board);
		//rank the moves by the number of points we can get to
		printBoard(board);
		val = isWin(board, col);
		int score =(val<<depth) + min(board, depth-1, col);
		board[col][row] = SPACE;
		return score;
	}
	
}



int makeMove(char board[columns+2][rows+2])
{
	int  i,tmp, max_move = -1, min_move = -1, max = -999999, min = 0;
	char piece;
	// Loop through all possible moves
	for(i=1;i<=columns;i++)
	{
		
		// Try placing a Blue piece
		tmp = tryMove(board, i, BLUE);
		#if DEBUG
		fprintf(f, "Trying col: %d - Piece: Blue - SCORE: %d\n\n", i, tmp);
		#endif
		if (tmp > max)
		{
			max_move = i;
			piece = BLUE;
			max = tmp;               
		}
		// Try placing a Green piece
		tmp = tryMove(board, i, GREEN);
		#if DEBUG
		fprintf(f, "Trying col: %d - Piece: Green - SCORE: %d\n\n", i, tmp);
		#endif
		if (tmp > max)
		{
			max_move = i;
			piece = GREEN;
			max = tmp;               
		}
		else if (tmp < min)
		{
			if(tmp == -99999)//row is full
				continue;
			min_move = i;
			min = tmp;               
		}
		
	}
	/*if(min < 0) // There's a Opponent's Win to Block
	fprintf(f, "(%d, %c)", min_move, BLUE);
	
	else
	*/
	
	printf("(%d, %c)", max_move, piece);
}


int checkLoss(char board[columns+2][rows+2])
{
	int i = 0, j = 0, row = 0, score = 0, max = 0, max_move = 0;
	char piece;

	
	for(i = 1; i <= columns; i++)
	{
		
		row = checkCol(board, i);
		
		board[i][row] = RED;
		//rank the moves by the number of points we can get to
		score = isWin(board, i);
#if DEBUG		
//		fprintf(f,"Col: %d, Row: %d, Score: %d\n", i, row, score);
#endif
		
		
		//if we are going to lose right away, place a blue to block;
		if(score < 0)
		{
			printf("(%d, b)", i);
			return 1;
		}
		
		board[i][row] = BLUE;
		//rank the moves by the number of points we can get to
		score = isWin(board, i);
		
#if DEBUG		
		fprintf(f,"Col: %d, Row: %d, Score: %d\n", i, row, score);
#endif
		
		
		//if we are going to lose right away, place a blue to block;
		if(score > 0)
		{
			if(score > max)
			{
				max_move = i;
				max = score;
				piece = BLUE;
			}
		}
		
		board[i][row] = GREEN;
		//rank the moves by the number of points we can get to
		score = isWin(board, i);
		
#if DEBUG		
		fprintf(f,"Col: %d, Row: %d, Score: %d\n", i, row, score);
#endif
		
		board[i][row] = SPACE;
		if(score < 0)
		{
			printf("(%d, b)", i);
			return 1;
		}
		else if(score > max)
		{
			max_move = i;
			piece = GREEN;
			max = score;
		}
		
		
		if(max)
		{
			printf("(%d, %c)", max_move, piece);
		return 1;
		}
	}
	return 0; 
}

//(10,7,5,0,0,0,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,r,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s)
//(10,7,7,2156,0,443,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,r,s,s,s,s,s,s,b,g,r,s,s,s,s,b,b,r,s,s,s,s,b,r,g,r,s,s,s,r,r,b,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s)

//(10,8,1,21490,150,10,b,b,g,r,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,r,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,r,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s)
//this board is one we need to get the min max to play a blue in the middle of the 2 reds in col 7 and 9

//(8,7,3,31925,5,31916,s,s,s,s,s,s,s,s,s,s,s,s,s,s,b,r,s,s,s,s,s,r,s,s,s,s,s,s,b,b,s,s,s,s,s,r,b,r,b,r,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s,s)
//on this board we are garunteed a win. The AI should play a G in 5, red will have to block, then a G in 4.
int main( int argc, char **argv)
{
	
	int p = 0;
	#if DEBUG
	char fname[32];
	#endif
	
	scanf("(%d,%d,%d,%d,%d,%d", &columns, &rows, &last_move, &total_time, &player_1_time, &last_move_time);
	#if DEBUG
	sprintf(fname, "./moves/game.out.%d.txt", player_1_time);
	f = fopen(fname, "w");
	#endif
	char board[columns+2][rows+2];
	p = readBoard(board);
	printBoard(board);


	/*********************************************/
	// For Debugging the Heuristic -Nick
	//printf("GUESS SCORE IS: %i\n", guess(board));
	//return 0;
	/********************************************/
	if(checkLoss(board))
	{
		#if DEBUG
		fclose(f);
		#endif
		return 0;
	}
	
	
	if(p == 0)
	{
		printf("(5, b)");
		#if DEBUG
		fclose(f);
		#endif
		return 0;
	}
	else if(p < 2)
	{
		if(last_move == 10)
			last_move-=2;
		printf("(%d, b)", last_move+1);
		
		#if DEBUG
		fclose(f);
		#endif
		return 0;
	}
	else if(p < 6)
	{
		printf("(%d, b)", last_move);
		#if DEBUG
		fclose(f);
		#endif
		return 0;
	}
	else if(p < 9)
	{
		if(last_move == 1)
			last_move += 2;
		printf("(%d, b)", last_move-1);
		#if DEBUG
		fclose(f);
		#endif
		return 0;
	}	  
	printBoard(board);
	makeMove(board);
	#if DEBUG
	fclose(f);
	#endif
	return 0;
}


